<?php

class ClientController extends CommonController {

	public function __construct() {
		parent::__construct();
	}

	public function index() {
		echo 'hoem index';
	}

	public function recordsAction() {
		$type = (int) $this->requestGet('type', 0);
		$flag = (int) $this->requestGet('flag', 2);
		$conditions = $type ? " AND `c`.`type`={$type} " : '';
		$conditions .= $flag < 2 ? " AND `c`.`flag`={$flag} " : '';

		$page = (int) $this->requestGet('page', 1);
		$pageSize = (int) $this->requestGet('pageSize', 100);
		$offset = ($page - 1) * $pageSize;

		$orderField = (int) $this->requestGet('orderField', 0);
		$orderDirection = (int) $this->requestGet('orderDirection', 1);
		$fields = array('c.type', 'c.name', 'c.api', 'c.sid', 'c.unid', 'c.server', 'c.flag');
		$field = isset($fields[$orderField]) ? $fields[$orderField] : $field[0];
		$direction = $orderDirection > 0 ? 'ASC' : 'DESC';
		$order = " ORDER BY {$field} {$direction} ";

		$totalQuery = "SELECT COUNT(`id`) as `total` FROM `client`";
		$db = $this->loadSqlite('bamboo');
		$totalRow = $db->getOne($totalQuery);
		$data = array('total' => $totalRow['total']);
		
		$query = "SELECT 
				`c`.*,
				`s`.`name` AS `serverName`,
				`s`.`url` AS `no3`
			FROM `client` AS `c` 
			LEFT JOIN `server` AS `s` 
				ON `c`.`server` = `s`.`id`
			WHERE 1 {$conditions} {$order} 
			LIMIT {$offset}, {$pageSize}";

		$data['records'] = $db->getAll($query);

		$this->jsonSuccess('', $data);
	}

	public function findAction() {
		$id = (int) $this->requestGet('id');
		if (!$id)
			$this->jsonError('参数错误');
		$findQuery = "SELECT * FROM `client` WHERE `id`={$id}";
		$db = $this->loadSqlite('bamboo');
		$row = $db->getOne($findQuery);
		$this->jsonSuccess('', $row);
	}

	public function findApiAction() {
		$api = (int) $this->requestGet('api');
		if (!$api)
			$this->jsonError('参数错误');
		$findQuery = "SELECT * FROM `client` WHERE `api`={$api} LIMIT 1";
		$db = $this->loadSqlite('bamboo');
		$row = $db->getOne($findQuery);
		$this->jsonSuccess('', $row);
	}


	public function setAction(){
		$id = (int) $this->requestPost('id');

		$data = array(
			'type' => (int) $this->requestPost('type'),
			'api' => (int) $this->requestPost('api'),
			'name' => (string) $this->requestPost('name'),
			'sid' => (int) $this->requestPost('sid'),
			'unid' => (int) $this->requestPost('unid'),
			'verify' => (string) $this->requestPost('verify'),
			'sig' => (string) $this->requestPost('sig'),
			'server' => (string) $this->requestPost('server'),
			'url' => (string) $this->requestPost('url'),
			'appId' => (string) $this->requestPost('appId'),
			'appKey' => (string) $this->requestPost('appKey'),
			'flag' => (int) $this->requestPost('flag')
		);

		$db = $this->loadSqlite('bamboo');

		if ($db->set('client', $data, $id)) {
			$this->jsonSuccess('保存成功');
		} else {
			$this->jsonError('保存失败 ' . $db->errorMessage);
		}
	}


	public function deleteAction() {
		$id = (int) $this->request('id');
		if (!$this->exists($id)) {
			$this->jsonError('保存失败');
		}
		$db = $this->loadSqlite('bamboo');
		$query = "DELETE FROM `client` WHERE `id`={$id}";
		if ($db->exec($query)) {
			$this->jsonSuccess('删除成功');
		} else {
			$this->jsonError('删除失败');
		}
	}

	private function exists($id) {
		$db = $this->loadSqlite('bamboo');
		$findQuery = "SELECT COUNT(*) as `count` FROM `client` WHERE `id`={$id}";
		$row = $db->getOne($findQuery);
		return $row['count'] > 0;
	}


	public function setSigAction(){
		die;
		$db = $this->loadSqlite('bamboo');
		$sigs = 'MPMTYKUGXLGHQWFOBZVYHJIHRCEKSTEJIGPJWXYZODSNABCDEFGHIJKLJGDACCVEAWQCPVDFCRSJKKWWJKNGQYGYPM';
		$query = "select * from client";
		$records = $db->getAll($query);
		foreach($records as $record){
			$api = $record['api'];
			$sig = $sigs{$api};
			$sql = "update client set letter='{$sig}' where api={$api}";
			$db->exec($sql);
		}
	}
}

?>
